package com.doujiao.auth.controller;

import com.doujiao.auth.config.RequiresPermission;
import com.doujiao.auth.service.AdminGroupService;
import com.doujiao.core.Result;
import com.doujiao.core.auth.dto.AdminGroupCreateDTO;
import com.doujiao.core.auth.dto.AdminGroupQueryDTO;
import com.doujiao.core.auth.dto.AdminGroupUpdateDTO;
import com.doujiao.core.auth.entity.AdminGroup;
import com.doujiao.core.auth.vo.AdminGroupPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员组控制器
 *
 * @author wuyunbin
 * @since 2025-05-24
 */
@Tag(name = "管理员组管理")
@RestController
@RequestMapping("/admin/group")
@RequiredArgsConstructor
public class AdminGroupController {

    private final AdminGroupService adminGroupService;

    @Operation(summary = "分页查询管理员组",description = "分页查询管理员组")
    @GetMapping("/page")
    @RequiresPermission("admin:group:page")
    public Result<AdminGroupPageVO> page(@Validated AdminGroupQueryDTO queryDTO) {
        AdminGroupPageVO pageVO = adminGroupService.page(queryDTO);
        return Result.success(pageVO);
    }

    @Operation(summary = "创建管理员组",description = "创建管理员组")
    @PostMapping
    @RequiresPermission("admin:group:create")
    public Result<AdminGroup> create(@Validated @RequestBody AdminGroupCreateDTO createDTO) {
        AdminGroup adminGroup = new AdminGroup();
        BeanUtils.copyProperties(createDTO, adminGroup);
        adminGroup = adminGroupService.create(adminGroup);
        return Result.success(adminGroup);
    }

    @Operation(summary = "更新管理员组",description = "更新管理员组")
    @PutMapping
    @RequiresPermission("admin:group:update")
    public Result<AdminGroup> update(@Validated @RequestBody AdminGroupUpdateDTO updateDTO) {
        AdminGroup adminGroup = new AdminGroup();
        BeanUtils.copyProperties(updateDTO, adminGroup);
        adminGroup = adminGroupService.update(adminGroup);
        return Result.success(adminGroup);
    }

    @Operation(summary = "删除管理员组",description = "删除管理员组")
    @DeleteMapping("/{id}")
    @RequiresPermission("admin:group:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = adminGroupService.delete(id);
        return Result.success(success);
    }

    @Operation(summary = "获取管理员组详情",description = "获取管理员组详情")
    @GetMapping("/{id}")
    @RequiresPermission("admin:group:info")
    public Result<AdminGroup> info(@PathVariable Long id) {
        AdminGroup adminGroup = adminGroupService.getById(id);
        return Result.success(adminGroup);
    }
}
